import userStore from "@/stores/store";
import axios from "../utils/request"
import path from "./path"
import { reactive } from "vue";
import router from "@/router";
import RecommendWork from "@/components/RecommendWork.vue";

const resultAnalysis = (result) =>{
    return result
    .then(res => {
        //console.log(res.data.msg);
        if(res.data.msg != null && res.data.msg == "NOT_LOGIN"){
            userStore().logout();
            router.push('/login');
        }
        return [0,res];
    })
    .catch(error => {
        if (error.response) {
            return [1,error.response];
        } 
        else if (error.request) {
            return [2,error.request];
        }
        else {
            return [3,error.message];
        }
    });
}
const getConfig = () => {
    const config = reactive({
        headers:{
            'token': userStore().getJWT,
        }
    })
    return config;
};
const getFormDataConfig = () => {
    const config = reactive({
        headers:{
        'token': userStore().getJWT,
        'Content-Type': 'multipart/form-data',
    }
})
    return config;
};
const api = {
    login(loginData) {
        return resultAnalysis(axios.post(path.baseUrl + path.loginUrl, loginData));
    },
    signup(account,name,sex,phoneNum,schSelect,password){
        return resultAnalysis( axios.post(path.baseUrl + path.signupUrl, {
            account: account,
            password: password,
            userName: name,
            phoneNum: phoneNum,
            schId: schSelect,
            sex: sex
        }));
    },
    getUniversity(){
        return resultAnalysis(axios.get(path.baseUrl+path.universityUrlAll));
    },
    getPermission(){
        return resultAnalysis(axios.get(path.baseUrl + path.permissionUrl,getConfig()));
    },
    getSelfInfo(){ 
        return resultAnalysis(axios.get(path.baseUrl + path.selfInfoUrl,getConfig()));
    },
    uploadImage(data){ 
        return resultAnalysis(axios.post(path.baseUrl + path.uploadImgUrl, data, getFormDataConfig()));
    },
    updateSelfInfo(data){
        return resultAnalysis(axios.put(path.baseUrl + path.selfInfoUrl, data, getConfig()));
    },
    changePassword(data){
        return resultAnalysis(axios.put(path.baseUrl + path.changePasswordUrl,data,getConfig()));
    },
    createClub(data){
        return resultAnalysis(axios.post(path.baseUrl + path.addClubUrl,data,getConfig()));
    },
    getSbIdName(data){
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl+path.getSbIdName,data),getConfig()));
    },
    getTransactionList(data){
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl+path.getTransListUrl,data),getConfig()));
    },
    applicateClub(data){
        return resultAnalysis(axios.post(path.baseUrl + path.applicationUrl,data,getConfig()));
    },
    affairAccept(data){
        return resultAnalysis(axios.put(buildParamsUrl(path.baseUrl+path.affairAcceptUrl,data),null,getConfig()));
    },
    affairReject(data){
        return resultAnalysis(axios.put(buildParamsUrl(path.baseUrl+path.affairRejectUrl,data),null,getConfig()));
    },
    getClubList(data){
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl+path.getClubListUrl, data), getConfig()));
    },
    sendMessage(data){
        return resultAnalysis(axios.post( path.baseUrl + path.sendMessageUrl, data, getConfig()));
    },
    getMessageList(){
        return resultAnalysis(axios.get(path.baseUrl + path.getMyMessageUrl, getConfig()));
    },
    joinClub(data){
        return resultAnalysis(axios.post(path.baseUrl + path.applicationUrl, data, getConfig()));
    },
    getSingleMsg(data){
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl + path.getSingleMsgUrl,data),getConfig()));
    },
    setMsgReaded(data){
        return resultAnalysis(axios.put(buildParamsUrl(path.baseUrl + path.setMsgReadedUrl ,data),null,getConfig()));
    },
    getManagedClub(){
        return resultAnalysis(axios.get(path.baseUrl + path.getManagedClubUrl,getConfig()));
    },
    getBasicClubInfo(data){  //获取某社团基本信息
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl+path.getBasicClubInfo,data),getConfig()))
    },
    applicationAffairs(data){
        return resultAnalysis(axios.post(path.baseUrl+path.affairWithFileUrl,data,getFormDataConfig()))
    },
    getClubJoined(){  //加入的社团
        return resultAnalysis(axios.get(path.baseUrl + path.MyClubJoinedUrl,getConfig()));
    },
    applicateExitClub(data){
        return resultAnalysis(axios.post(path.baseUrl + path.applicationUrl, data, getConfig()));
    },
    updateClubInfo(data){  //直接修改社团信息
        return resultAnalysis(axios.put(path.baseUrl + path.updateClubInfoUrl,data ,getFormDataConfig()))
    },
    uploadWorks(data){  //上传社团作品
        return resultAnalysis(axios.post(path.baseUrl + path.uploadWorksUrl, data, getFormDataConfig()))
    },
    getMemberList(permission,data){  //获取人员列表
        if(permission == 1 || permission == 0){  
            return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl+path.getPersonListCommitUrl,data),getConfig()));
        }
        else if(permission == 2){  
            return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl+path.getPersonListCommonUrl,data),getConfig()));
        }
    },
    getSelfWorksList(data){   //获取作品列表
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl+path.getSelfWorksListUrl,data),getConfig()));
    },
    getAffairInfo(data){//获取事务的修改后的信息
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl + path.affairInfoUrl,data),getConfig()))
    },
    getWorksInfo(data){  
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl+path.getWorksInfoUrl,data),getConfig()))
    },
    getArticle(url){
        return resultAnalysis(axios.get(url));
    },
    getPelpleInfo(data){
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl+path.getPeopleInfoUrl,data),getConfig()))
    },
    resetPassword(data){
        return resultAnalysis(axios.put(buildParamsUrl(path.baseUrl + path.resetPasswordUrl ,data),null,getConfig()))
    },
    deleteClub(data){
        return resultAnalysis(axios.delete(buildParamsUrl(path.baseUrl + path.deleteClubUrl,data),getConfig()))
    },
    getPeopleJoinedClub(data){
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl + path.getPeopleJoinedClubUrl ,data),getConfig()))
    },
    deleteArticle(data){
        return resultAnalysis(axios.delete(buildParamsUrl(path.baseUrl + path.deleteArticle,data),getConfig()))
    },
    RecommendWork(data){
        return resultAnalysis(axios.put(buildParamsUrl(path.baseUrl + path.recommendUrl ,data),null,getConfig()))
    },
    getWorkList(data){
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl + path.allWorkListUrl,data),getConfig()))
    },
    getClubViewList(data){
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl + path.getClubViewListUrl,data),getConfig()));
    },
    getHomepageMultimedia(data){
        return resultAnalysis(axios.get(buildParamsUrl(path.baseUrl + path.homepageWultiMediaUrl,data),getConfig()))
    }
}
//构建get方式url
const buildParamsUrl = (url,data) => {
    let newUrl = url;
    const queryParams = Object.keys(data)
    .map(key => {
      return `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`;
    })
    .join('&');
    newUrl = newUrl + '?' +queryParams;
    //console.log(newUrl); //debug
    return newUrl;
}
export default api;